package com.nuanshui.heatedloan.order.integration.execute.handler.syshandler;

import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.order.integration.annotation.OrderExecuteAnnotation;
import com.nuanshui.heatedloan.order.integration.bean.OrderResult;
import com.nuanshui.heatedloan.order.integration.constant.OrderHandlerOrderNum;
import com.nuanshui.heatedloan.order.integration.execute.handler.OrderHandler;
import com.nuanshui.heatedloan.order.integration.form.OrderExecuteForm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;

/**
 * 同步 handler
 * @Author: Gonzo
 * @Date 2017/4/13下午5:57
 * @since 1.4.2
 */
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
@OrderExecuteAnnotation(value="synchronizedHandler", order = OrderHandlerOrderNum.ORDER_SYN)
public class SynchronizedHandler implements OrderHandler {

    private OrderHandler nextOrderhandler;

    private Logger logger = LoggerFactory.getLogger("appointment_order");


    @Override
    public OrderResult placeOrder(OrderExecuteForm form, OrderResult resp) {
        StringBuilder sb = new StringBuilder();
        LogUtil.info(logger, String.format("----------生成订单, 使用的同步值是:%s----------", sb.toString()));
        synchronized (sb.toString().intern()) {
            nextOrderhandler.placeOrder(form, resp);
        }

        return resp;
    }

    @Override
    public OrderResult paySuccessOrder(OrderExecuteForm form, OrderResult result) {
        return nextOrderhandler.paySuccessOrder(form, result);
    }

    @Override
    public OrderResult cancelOrder(OrderExecuteForm form, OrderResult result) {
        return nextOrderhandler.cancelOrder(form, result);
    }

    @Override
    public OrderResult verifyAppointmentOrder(OrderExecuteForm form, OrderResult result) {
        return nextOrderhandler.verifyAppointmentOrder(form, result);
    }

    @Override
    public void setNextOrderHandler(OrderHandler orderHandler) {
        this.nextOrderhandler = orderHandler;
    }
}
